package palf {
import flash.display.DisplayObject;
import flash.display.Sprite;

import org.flexunit.asserts.assertFalse;
import org.flexunit.asserts.assertStrictlyEquals;
import org.flexunit.asserts.assertTrue;

public class IndexedSpriteTest {

	private var indexed : IndexedSprite;
	private var childOne : DisplayObject;
	private var childTwo : DisplayObject;
	private var childThree : DisplayObject;

	public function IndexedSpriteTest() {

	}

	[Before]
	public function setup() : void {
		indexed = new IndexedSprite();
		childOne = new Sprite();
		childTwo = new Sprite();
		childThree = new Sprite();
	}

	//------------------------------------------------------------//

	[Test]
	public function indexedSpriteIsSprite() : void {
		assertTrue(indexed is Sprite);
	}

	//----------------------------//

	[Test]
	public function addChild() : void {
		assertFalse(indexed.contains(childOne));
		assertStrictlyEquals(0, indexed.numChildren);

		indexed.addChild(childOne);
		assertTrue(indexed.contains(childOne));
		assertStrictlyEquals(1, indexed.numChildren);
	}

	[Test]
	public function addingSameChildTwice() : void {
		indexed.addChild(childOne);
		indexed.addChild(childOne);

		assertStrictlyEquals(0, indexed.getChildIndex(childOne));
		assertStrictlyEquals(1, indexed.numChildren);
	}

	[Test]
	public function addingDifferentChildren() : void {
		indexed.addChild(childOne);
		indexed.addChild(childTwo);

		assertStrictlyEquals(0, indexed.getChildIndex(childOne));
		assertStrictlyEquals(1, indexed.getChildIndex(childTwo));
		assertStrictlyEquals(2, indexed.numChildren);
	}

	[Test]
	public function addingChildOneTwiceAroundChildTwoMovesChildOne() : void {
		indexed.addChild(childOne);
		indexed.addChild(childTwo);
		indexed.addChild(childOne);

		assertStrictlyEquals(0, indexed.getChildIndex(childTwo));
		assertStrictlyEquals(1, indexed.getChildIndex(childOne));
		assertStrictlyEquals(2, indexed.numChildren);
	}

	//----------------------------//

	[Test]
	public function addChildAt() : void {
		indexed.addChild(childOne);
		indexed.addChildAt(childTwo, 0);
		assertStrictlyEquals(2, indexed.numChildren);
		assertStrictlyEquals(1, indexed.getChildIndex(childOne));
		assertStrictlyEquals(0, indexed.getChildIndex(childTwo));
	}

	//------------------------------------------------------------//

	[Test]
	public function removeChild() : void {
		indexed.addChild(childOne);
		indexed.addChild(childTwo);

		indexed.removeChild(childOne);
		assertFalse(indexed.contains(childOne));
		assertTrue(indexed.contains(childTwo));
		assertStrictlyEquals(1, indexed.numChildren);
	}

	[Test]
	public function removingChildThatIsNotPresent() : void {
		indexed.addChild(childOne);
		indexed.addChild(childTwo);

		indexed.removeChild(childOne);
		indexed.removeChild(childOne);
		assertFalse(indexed.contains(childOne));
		assertTrue(indexed.contains(childTwo));
		assertStrictlyEquals(1, indexed.numChildren);
	}

}
}
